/*
 * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
 * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
 * CONSEQUENTLY, CMOSTEK SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * Copyright (C) CMOSTEK SZ.
 */

/*!
 * @file    cmt2300a_hal.c
 * @brief   CMT2300A hardware abstraction layer
 *
 * @version 1.2
 * @date    Jul 17 2017
 * @author  CMOSTEK R@D
 */
 
#include "cmt2300a_hal.h"
#include "cmt_spi3.h"



void GPIO_Pin_Setting(GPIO_TypeDef *gpio, uint16_t nPin, uint32_t speed, uint32_t mode)
{
#if 1
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.Pin = nPin;
    GPIO_InitStructure.Speed = speed;
    GPIO_InitStructure.Mode = mode;
    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
#else
    u16 i;
    u32 nCfg, nMask = 0x0F;
    
    nCfg  = (mode&0x10) ?speed :0;
    nCfg |= mode & 0x0C;
    
    if(nPin == 0)
        return;
    
    if(nPin < 0x0100)
    {
        for(i=nPin; (0x01&i)==0; i >>= 1) {
            nCfg <<= 4;
            nMask <<= 4;
        }
        
        gpio->CRL &= ~nMask;
        gpio->CRL |= nCfg;
    }
    else
    {
        for(i=(nPin>>8); (0x01&i)==0; i >>= 1) {
            nCfg <<= 4;
            nMask <<= 4;
        }
        
        gpio->CRH &= ~nMask;
        gpio->CRH |= nCfg;
    }
    
    if(GPIO_Mode_IPD==mode)
        gpio->BRR = nPin;
    
    else if(GPIO_Mode_IPU==mode)
        gpio->BSRR = nPin;
    
#endif
}

/*! ********************************************************
* @name    CMT2300A_InitGpio
* @desc    Initializes the CMT2300A interface GPIOs.
* *********************************************************/
void CMT2300A_InitGpio(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

 /*Configure GPIO pins : PBPin PBPin */
  GPIO_InitStruct.Pin = CMT_GPIO1_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(CMT_GPIO1_GPIO, &GPIO_InitStruct);	

  /*Configure GPIO pin : PtPin   TEST*/
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);		
	
 /*Configure GPIO pins : PBPin PBPin */
  GPIO_InitStruct.Pin = CMT_GPIO2_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(CMT_GPIO2_GPIO, &GPIO_InitStruct);	

 /*Configure GPIO pins : PBPin PBPin */
  GPIO_InitStruct.Pin = CMT_GPIO3_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(CMT_GPIO3_GPIO, &GPIO_InitStruct);	
	
  /*Configure GPIO pin : PtPin   TEST*/
  GPIO_InitStruct.Pin = CMT_CSB_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(CMT_CSB_GPIO, &GPIO_InitStruct);		
	
  /*Configure GPIO pin : PtPin   TEST*/
  GPIO_InitStruct.Pin = CMT_FCSB_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(CMT_FCSB_GPIO, &GPIO_InitStruct);			

  /*Configure GPIO pin : PtPin   TEST*/
  GPIO_InitStruct.Pin = CMT_SCLK_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
//  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(CMT_SCLK_GPIO, &GPIO_InitStruct);		
	
  /*Configure GPIO pin : PtPin   TEST*/
  GPIO_InitStruct.Pin = CMT_SDIO_GPIO_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(CMT_SDIO_GPIO, &GPIO_InitStruct);		

  cmt_spi3_delay();
//		CMT2300A_SetGpio1In();
//    CMT2300A_SetGpio2In();
//    CMT2300A_SetGpio3In();
//	
//    cmt_spi3_init();
}

/*! ********************************************************
* @name    CMT2300A_ReadReg
* @desc    Read the CMT2300A register at the specified address.
* @param   addr: register address
* @return  Register value
* *********************************************************/
u8 CMT2300A_ReadReg(u8 addr)
{
    u8 dat = 0xFF;
    cmt_spi3_read(addr, &dat);
	
    return dat;
}

/*! ********************************************************
* @name    CMT2300A_WriteReg
* @desc    Write the CMT2300A register at the specified address.
* @param   addr: register address
*          dat: register value
* *********************************************************/
void CMT2300A_WriteReg(u8 addr, u8 dat)
{
    cmt_spi3_write(addr, dat);
}

/*! ********************************************************
* @name    CMT2300A_ReadFifo
* @desc    Reads the contents of the CMT2300A FIFO.
* @param   buf: buffer where to copy the FIFO read data
*          len: number of bytes to be read from the FIFO
* *********************************************************/
void CMT2300A_ReadFifo(u8 buf[], u16 len)
{
    cmt_spi3_read_fifo(buf, len);
}

/*! ********************************************************
* @name    CMT2300A_WriteFifo
* @desc    Writes the buffer contents to the CMT2300A FIFO.
* @param   buf: buffer containing data to be put on the FIFO
*          len: number of bytes to be written to the FIFO
* *********************************************************/
void CMT2300A_WriteFifo(const u8 buf[], u16 len)
{
    cmt_spi3_write_fifo(buf, len);
}
